<?php /** @noinspection ALL */


namespace app\ulquiorra\controller;

use think\Controller;
use think\Db;
use think\facade\Request;
use think\facade\Session;


class Login extends Controller {

	function login(){
		if(Request::isGet()){
			return $this->fetch();
		}
		if(Request::isPost()){
			$input=input('post.');
			$a=Db::table('account')->getBylogin($input['login']);
			if(empty($a)){
				return json(['status'=>false,'message'=>'用户名不存在']);
			}
			if($a['password']!=$input['password']){
				return json(['status'=>false,'message'=>'密码不正确']);
			}
			Session::set('id',$a['id']);
			return json(['status'=>true,]);
		}
	}

	function zhuce(){
		if(Request::isGet()){
			return $this->fetch();
		}
		if(Request::isPost()){
			$a=input('post.');
			$b=Db::table('account')->getBylogin($a['login']);
			if(!empty($b)){
				return json(['status'=>false,'message'=>'该账号已被注册']);
			}else{
				$data = ['name' => $a['name'],'password'=>$a['password'],'login'=>$a['login']];
				Db::name('account')->insert($data);
				return json(['status'=>true]);
			}
		}
	}

	function index($page=1,$page1=1){
		if(Request::isGet()){
			if(Session::get('id')==null){
				echo $this->error('请先登录','Login/login');
			}else{


				$name=Db::table('account')->getFieldByid(Session::get('id'),"name");

				$data1=Db::table('book_type')->select();

				@$book_type=$_REQUEST['book_type'];
				if($book_type=='all'){$like=Db::table('books')->page($page,10)->select();}else{
					$like=Db::name('books')->whereLike('book_type','%'.$book_type.'%')->page($page,10)->select();
				}

				$vip=Db::table('vip')->page($page1,13)->select();

				$itemCount=Db::table('books')->count();
				$pageCount=ceil($itemCount/10);
				$pageStart=$page-2;
				$pageStart=$pageStart<1?1:$pageStart;
				$pageEnd=$page+2;
				$pageEnd=$pageEnd>$pageCount?$pageCount:$pageEnd;

				$itemCount1=Db::table('vip')->count();
				$pageCount1=ceil($itemCount1/13);
				$pageStart1=$page1-2;
				$pageStart1=$pageStart1<1?1:$pageStart1;
				$pageEnd1=$page1+2;
				$pageEnd1=$pageEnd1>$pageCount1?$pageCount1:$pageEnd1;

				$this->assign('vip',$vip);
				$this->assign('name',$name);
				$this->assign('data1',$data1);
				$this->assign('like',$like);
				$this->assign( 'pageCount',$pageCount);
				$this->assign( 'page',$page);
				$this->assign( 'pageStart',$pageStart);
				$this->assign( 'pageEnd',$pageEnd);

				$this->assign( 'pageCount1',$pageCount1);
				$this->assign( 'page1',$page1);
				$this->assign( 'pageStart1',$pageStart1);
				$this->assign( 'pageEnd1',$pageEnd1);
				return $this->fetch();
			}
		}
		if(Request::isPost()){
			@$inp=$_REQUEST['inp'];
			$like=Db::name('books')->whereLike('book_name','%'.$inp.'%')->page($page,10)->select();
			$name=Db::table('account')->getFieldByid(Session::get('id'),"name");
			$data1=Db::table('book_type')->select();
			$vip=Db::table('vip')->page($page1,13)->select();

			$itemCount=Db::table('books')->count();
			$pageCount=ceil($itemCount/10);
			$pageStart=$page-2;
			$pageStart=$pageStart<1?1:$pageStart;
			$pageEnd=$page+2;
			$pageEnd=$pageEnd>$pageCount?$pageCount:$pageEnd;

			$itemCount1=Db::table('vip')->count();
			$pageCount1=ceil($itemCount1/13);
			$pageStart1=$page1-2;
			$pageStart1=$pageStart1<1?1:$pageStart1;
			$pageEnd1=$page1+2;
			$pageEnd1=$pageEnd1>$pageCount1?$pageCount1:$pageEnd1;

			$this->assign('data1',$data1);
			$this->assign('like',$like);
			$this->assign('name',$name);
			$this->assign('vip',$vip);
			$this->assign( 'pageCount',$pageCount);
			$this->assign( 'page',$page);
			$this->assign( 'pageStart',$pageStart);
			$this->assign( 'pageEnd',$pageEnd);

			$this->assign( 'pageCount1',$pageCount1);
			$this->assign( 'page1',$page1);
			$this->assign( 'pageStart1',$pageStart1);
			$this->assign( 'pageEnd1',$pageEnd1);
			return $this->fetch();
		}
	}

	function shangjia(){
		if(Request::isGet()){
			$name=Db::table('account')->getFieldByid(Session::get('id'),"name");
			$data=Db::table('book_type')->select();
			$this->assign('name',$name);
			$this->assign('data',$data);
			return $this->fetch();
		}
		if(Request::isPost()){
			$book_name=$_REQUEST['book_name'];
			$book_writer=$_REQUEST['book_writer'];
			$book_chuban=$_REQUEST['book_chuban'];
			$book_money=$_REQUEST['book_money'];
			$book_time=date('Y/m/d',time());
			$file=$_FILES['file'];
			$book_type=$_REQUEST['book_type'];
			if(empty($book_name)||empty($book_writer)||empty($book_chuban)||empty($book_money)||empty($file)){
				return json(['status'=>false,'message'=>'不能为空']);
				die;
			}else{
				$b=Db::table('books')->getBybook_name($book_name);
				if(!empty($b)){
					return json(['status'=>false,'message'=>'该书名已存在']);
					die;
				}

				move_uploaded_file($file['tmp_name'],"./static/update_image/".$file['name']);
				$book_url="./static/update_image/".$file["name"];

				$data = [
					'book_name' =>$book_name,
					'book_writer'=>$book_writer,
					'book_chuban'=>$book_chuban,
					'book_money'=>$book_money,
					'book_time'=>$book_time,
					'book_type'=>$book_type,
					'book_url'=>$book_url
				];
				Db::name('books')->insert($data);
				return json(['status'=>true]);
			}
		}
	}

	function fenlei(){
		if(Request::isGet()){
			$name=Db::table('account')->getFieldByid(Session::get('id'),"name");
			$this->assign('name',$name);
			return $this->fetch();
		}
		if(Request::isPost()){
			$input=input('post.');
			if(empty($input['type'])){
				return json(['status'=>false,'message'=>'不能为空']);
				die();
			}
			$a=Db::table('book_type')->getBytype($input['type']);
			if(!empty($a)){
				return json(['status'=>false,'message'=>'该类型已存在']);
			}else{
				$data = ['type' => $input['type']];
				Db::name('book_type')->insert($data);
				return json(['status'=>true]);
			}
		}
	}

	function books($page=1){
		if(Request::isGet()){
			$itemCount=Db::table('books')->count();
			$pageCount=ceil($itemCount/8);

			$pageStart=$page-8;
			$pageStart=$pageStart<1?1:$pageStart;

			$pageEnd=$page+8;
			$pageEnd=$pageEnd>$pageCount?$pageCount:$pageEnd;


			$name=Db::table('account')->getFieldByid(Session::get('id'),"name");

			return view('',[
				'ops'=>Db::table('books')->page($page,8)->select(),
				'pageCount'=>$pageCount,
				'page'=>$page,
				'pageStart'=>$pageStart,
				'pageEnd'=>$pageEnd,
				'name'=>$name
			]);
		}
	}

	function book_bianji(){
		if(Request::isGet()){
			$id=$_REQUEST['id'];
			$name=Db::table('account')->getFieldByid(Session::get('id'),"name");
			$data=Db::table('books')->getByid($id);
			$data1=Db::table('book_type')->select();
			$this->assign('name',$name);
			$this->assign('data',$data);
			$this->assign('data1',$data1);
			return $this->fetch();
		}
		if(Request::isPost()){
			$id=$_REQUEST['id'];
			$book_name=$_REQUEST['book_name'];
			$book_writer=$_REQUEST['book_writer'];
			$book_chuban=$_REQUEST['book_chuban'];
			$book_money=$_REQUEST['book_money'];
			$book_time=date('Y/m/d',time());
			$file=$_FILES['file'];
			$book_type=$_REQUEST['book_type'];
			if(empty($book_name)||empty($book_writer)||empty($book_chuban)||empty($book_money)||empty($file)){
				return json(['status'=>false,'message'=>'不能为空']);
				die;
			}else {
				$b = Db::table('books')->getBybook_name($book_name);
				if (!empty($b)) {
					return json(['status' => false, 'message' => '该书名已存在']);
					die;
				}

				move_uploaded_file($file['tmp_name'],"./static/update_image/".$file['name']);
				$book_url="./static/update_image/".$file["name"];

				Db::name('books')->where('id', $id)
					->update([
						'book_name' => $book_name,
						'book_writer'=>$book_writer,
						'book_chuban'=>$book_chuban,
						'book_money'=>$book_money,
						'book_time'=>$book_time,
						'book_type'=>$book_type,
						'book_url'=>$book_url
					]);
				return json(['status'=>true]);
			}
		}
	}

	function delete(){
		@$id=$_REQUEST['id'];
		Db::table('books')->delete($id);
		echo $this->success('删除成功','Login/books');
	}

	function vip_zhuce(){
		if(Request::isGet()){
			$name=Db::table('account')->getFieldByid(Session::get('id'),"name");
			$this->assign('name',$name);
			return $this->fetch();
		}
		if(Request::isPost()){
			$name=$_REQUEST['name'];
			$tel=$_REQUEST['tel'];
			$vip_time=date('Y/m/d',time());
			if(empty($name)||empty($tel)){
				return json(['status'=>false,'message'=>'不能为空']);
				die;
			}else{
				$data=Db::table('vip')->getBytel($tel);
				if(!empty($data)){
					return json(['status'=>false,'message'=>'该电话已被注册']);
					die;
				}
				$data1 = ['name' => $name,'tel'=>$tel,'vip_time'=>$vip_time];
				Db::name('vip')->insert($data1);
				return json(['status'=>true]);
			}
		}
	}

	function vip_chong(){
		if(Request::isGet()){
			$name=Db::table('account')->getFieldByid(Session::get('id'),"name");
			$this->assign('name',$name);
			return $this->fetch();
		}
		if(Request::isPost()){
			$tel=$_REQUEST['tel'];
			$money=$_REQUEST['money'];
			if(empty($tel)){
				return json(['status'=>false,'message'=>'不能为空']);
				die;
			}else{
				$a=Db::table('vip')->getBytel($tel);
				if(empty($a)){
					return json(['status'=>false,'message'=>'该号码是空号']);
					die;
				}
				$moneys=$a['money']+$money;
				Db::name('vip')->where('tel', $tel)->update(['money' => $moneys]);
				return json(['status'=>true]);
			}
		}
	}

	function vip_biao($page=1){
		if(Request::isGet()){
			$name=Db::table('account')->getFieldByid(Session::get('id'),"name");
			$this->assign('name',$name);

			$itemCount=Db::table('vip')->count();
			$pageCount=ceil($itemCount/10);

			$pageStart=$page-10;
			$pageStart=$pageStart<1?1:$pageStart;

			$pageEnd=$page+10;
			$pageEnd=$pageEnd>$pageCount?$pageCount:$pageEnd;


			return view('',[
				'ops'=>Db::table('vip')->page($page,10)->select(),
				'pageCount'=>$pageCount,
				'page'=>$page,
				'pageStart'=>$pageStart,
				'pageEnd'=>$pageEnd
			]);
		}
	}

	function vip_bianji(){
		if(Request::isGet()) {
			$name = Db::table('account')->getFieldByid(Session::get('id'), "name");

			$id=$_REQUEST['id'];
			$data=Db::table('vip')->getByid($id);

			$this->assign('name', $name);
			$this->assign('data', $data);
			return $this->fetch();
		}
		if(Request::isPost()){
			$name=$_REQUEST['name'];
			$tel=$_REQUEST['tel'];
			$id=$_REQUEST['id'];
			$vip_time=date('Y/m/d',time());
			if(empty($name)||empty($tel)){
				return json(['status'=>false,'message'=>'不能为空']);
				die;
			}else{
				$a=Db::table('vip')->getBytel($tel);
				if(!empty($a)){
					return json(['status'=>false,'message'=>'该号码已被注册']);
					die;
				}
				Db::name('vip')->where('id', $id)->update(['name'=>$name,'tel'=>$tel,'vip_time'=>$vip_time]);
				return json(['status'=>true]);
			}
		}
	}

	function vip_delete(){
		@$id=$_REQUEST['id'];
		Db::table('vip')->delete($id);
		echo $this->success('删除成功','Login/vip_biao');
	}

	function about(){
		$name = Db::table('account')->getFieldByid(Session::get('id'), "name");
		$this->assign('name', $name);
		return $this->fetch();
	}

	function work(){
		$name = Db::table('account')->getFieldByid(Session::get('id'), "name");
		$data=Db::table('account')->column('name');
		$this->assign('data', $data);
		$this->assign('name', $name);
		return $this->fetch();
	}

	function out(){
		Session::delete('id');
		echo $this->success('您已退出登录','Login/login');
	}

	function update(){
		if(Request::isGet()){
			$name = Db::table('account')->getFieldByid(Session::get('id'), "name");
			$this->assign('name', $name);
			return $this->fetch();
		}
		if(Request::isPost()){
			$paw=$_REQUEST['paw'];
			$password=$_REQUEST['password'];
			if(empty($paw)||empty($password)){
				return json(['status'=>false,'message'=>'不能为空']);
				die;
			}else{
				$a=Db::table('account')->getByid(Session::get('id'));
				if($paw!=$a['password']){
					return json(['status'=>false,'message'=>'旧密码错误']);
					die;
				}
				Db::name('account')->where('id', Session::get('id'))
					->update(['password' => $password]);
				return json(['status'=>true]);
			}
		}
	}

	function zhuxiao(){
		Db::table('account')->delete(Session::get('id'));
		echo $this->success('您已注销该账号','Login/login');
	}

	function lend(){
		if(Request::isGet()){
			$books_id=$_REQUEST['books_id'];
			$vip_id=$_REQUEST['vip_id'];
			if($books_id!=null||$vip_id!=null){
				$data = ['vip_id' => $vip_id,'books_id'=>$books_id,'lend_time'=>date('Y/m/d',time())];
				Db::name('lend')->insert($data);
			}

			$a=Db::query("select * from books join lend on lend.books_id=books.id where lend.vip_id=".$vip_id);
			Session::delete('vip_id');
			return json([$a]);
		}
	}

	function lend1(){
		if(Request::isGet()){
			@$vip_id=$_REQUEST["vip_id"];
			Session::set('vip_id',$vip_id);
			$a=Db::query("select * from books join lend on lend.books_id=books.id where lend.vip_id=".Session::get('vip_id'));
			Session::delete('vip_id');
			return json(['status'=>true,'a'=>[$a]]);
		}
	}

	function tetsas(){
		Db::query("select * from aaa");
	}
}